﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhiJiao.Dal;
using ZhiJiao.Models.Entities;
using ZhiJiao.Models.Entities.Enums;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;

namespace ZhiJiao.Service.Services
{
    public class CourseService:ICourseService
    {
        private readonly ICurrentUser _currentUser;
        private readonly ZJDbContext _db;
        private readonly BackJson _res;
        private readonly IMapper _mapper;

        public CourseService(ICurrentUser currentUser,
                              ZJDbContext db,
                              BackJson res,
                              IMapper mapper)
        { 
            _db = db;
            _currentUser = currentUser;
            _res = res;
            _mapper = mapper;
        }

        /// <summary>
        /// 当前机构下课程列表
        /// </summary>
        public async Task<BackJson> GetCourseList(string? name,CourseType? type)
        {
            var query =  _db.Courses.AsQueryable();
            query = query.Where(c => c.Instruction == _currentUser.User.Instruction);
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(c => c.Name.Contains(name));
            }
            if (type != null)
            {
                query = query.Where(c => c.CourseType == type);
            }
            var list = await query.ToListAsync();
            _res.code = 0;
            _res.data = _mapper.Map<List<Course>, List<CourseDto>>(list);
            return _res;
        }

        /// <summary>
        /// 新建课程
        /// </summary>
        public async Task<BackJson> NewCourse()
        {
            try
            {
                var course = new Course();
                course.Name = "新建课程";
                course.Instruction = _currentUser.User.Instruction;
                _db.Courses.Add(course);
                _res.code = 0;
                _res.data = _mapper.Map<Course, CourseDto>(course);
            }
            catch (Exception e)
            { 
                _res.code = -1;
                _res.msg = e.Message;
            }
           return _res;
        }

        
        /// <summary>
        /// 获取单个课程
        /// </summary>
        public async Task<BackJson> GetCourse(long id)
        {
            try
            {
                var course = await _db.Courses.FirstOrDefaultAsync(c => c.Id == id);
                if (course != null)
                {
                    _res.code = 0;
                    _res.data = _mapper.Map<Course, CourseDto>(course);
                }
                else
                {
                    _res.code = 1;
                    _res.msg = "课程不存在或已被删除";
                }
            }
            catch (Exception e)
            {
                _res.code = 1;
                _res.msg = e.Message;
            }
            return _res;
        }

        /// <summary>
        /// 保存课程
        /// </summary>
        /// <returns></returns>
        public async Task<BackJson> SaveCourse([FromBody]CourseDto dto)
        {
            if (dto != null)
            {
                var course = await _db.Courses.FirstOrDefaultAsync(c=>c.Id==dto.Id);
                if (course != null)
                {
                    course = _mapper.Map<CourseDto, Course>(dto);
                    _db.Courses.Update(course);
                    await _db.SaveChangesAsync();
                    _res.code = 0;
                    _res.msg = "课程保存成功";
                }
                else
                {
                    _res.code = 0;
                    _res.msg = "您所编辑的课程不存在或已被删除";
                }
            }
            else
            {
                _res.code = 0;
                _res.msg = "模型绑定失败";
            }
            return _res;
        }

        /// <summary>
        ///  删除课程
        /// </summary>
        public async Task<BackJson> DeleteCourse(long id)
        {
            var course = await _db.Courses.FirstOrDefaultAsync(c=>c.Id==id);
            if (course != null)
            {
                _db.Courses.Remove(course);
                await _db.SaveChangesAsync();
                _res.code = 0;
                _res.msg = "课程删除成功";
            }
            else
            {
                _res.code = 1;
                _res.msg = "课程删除成功";
            }
            return _res;
        }
    }
}
